package com.monicash.monicash.information.fragment;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.monicash.monicash.R;
import com.monicash.monicash.base.BaseFragment;
import com.monicash.monicash.bean.AppKycPersonalInformacionConfig;
import com.monicash.monicash.bean.ZiLiPageUserDataBean;
import com.monicash.monicash.information.CapitalInforActivity;
import com.monicash.monicash.information.modle.KycModle;
import com.monicash.monicash.util.T;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class PersonalInformationFragment extends BaseFragment {

    EditText et_nombre;
    TextView tv_nombre;
    LinearLayout ll_nombre;
    EditText et_father_surname;
    LinearLayout ll_father_surname;
    TextView tv_father_surname;
    TextView tv_father_surname_err;


    EditText et_mother_surname;
    LinearLayout ll_mother_surname;
    TextView tv_mother_surname;

    EditText et_runs;
    TextView tv_runs;
    LinearLayout ll_runs;

    TextView et_birth;
    ImageView iv_birth;
    EditText et_personal_email;
    LinearLayout ll_personal_email;
    TextView tv_personal_email;

    ConstraintLayout cl_sexo;
    TextView tv_sexo;
    LinearLayout ll_sex_man;
    LinearLayout ll_sex_woman;
    TextView tv_junior;
    TextView tv_preparatory;
    TextView tv_license;
    TextView tv_learned_person;
    TextView tv_absence;
    TextView tv_has_some;
    TextView tv_not_have;
    TextView tv_submit_to;
    View view = null;

    int sex = 0;// 0 man 1 woman
    int educationOn = 0;// 默认值
    int hasSome = 0;//默认值
    public KycModle kycModle = null;

    private String firstName;
    private String middleName;
    private String lastMotherName;
    private String nationalId;
    private String birthday;
    private String gender;
    private String educationType;
    private String email;
    private String debt;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_personal_information, container, false);
        kycModle = KycModle.Companion.getInstant();
        initView();
        initData();
        return view;
    }

    @Override
    public void initView() {
        findViewById();
        sexSetting();
        educationOnClick();

        iv_birth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        String dateString = year+"-"+month+"-"+dayOfMonth;
                        et_birth.setText(dateString);
                    }
                },year,month,day);
                datePickerDialog.show();
            }
        });

        tv_has_some.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getActivity()==null){
                    return;
                }
                hasSome = 1;
                tv_has_some.setBackground(getActivity().getDrawable(R.drawable.bg_gradient_65ff96_00daf7_corner_0));
                tv_not_have.setBackground(getActivity().getDrawable(R.drawable.bg_frame_4d333333_corner_8));
            }
        });
        tv_not_have.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getActivity()==null){
                    return;
                }
                hasSome = 2;
                tv_not_have.setBackground(getActivity().getDrawable(R.drawable.bg_gradient_65ff96_00daf7_corner_0));
                tv_has_some.setBackground(getActivity().getDrawable(R.drawable.bg_frame_4d333333_corner_8));
            }
        });
        tv_submit_to.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userupdateUserInfo();
            }
        });

    }

    public void hasSmome(){
        if(hasSome == 1){
            tv_has_some.setBackground(getActivity().getDrawable(R.drawable.bg_gradient_65ff96_00daf7_corner_0));
            tv_not_have.setBackground(getActivity().getDrawable(R.drawable.bg_frame_4d333333_corner_8));
        }else if(hasSome == 2){
            tv_not_have.setBackground(getActivity().getDrawable(R.drawable.bg_gradient_65ff96_00daf7_corner_0));
            tv_has_some.setBackground(getActivity().getDrawable(R.drawable.bg_frame_4d333333_corner_8));
        }



    }

    @Override
    public void initData() {
        getZiLiPageUserFullInfo();
        getZiLiPageUserInfoConfig();

    }

    private void findViewById(){
        et_nombre = view.findViewById(R.id.et_nombre);
        tv_nombre = view.findViewById(R.id.tv_nombre);
        ll_nombre = view.findViewById(R.id.ll_nombre);

        et_father_surname = view.findViewById(R.id.et_father_surname);
        tv_father_surname_err = view.findViewById(R.id.tv_father_surname_err);
        ll_father_surname = view.findViewById(R.id.ll_father_surname);
        tv_father_surname = view.findViewById(R.id.tv_father_surname);

        et_mother_surname = view.findViewById(R.id.et_mother_surname);
        ll_mother_surname = view.findViewById(R.id.ll_mother_surname);
        tv_mother_surname = view.findViewById(R.id.tv_mother_surname);

        et_runs = view.findViewById(R.id.et_runs);
        tv_runs = view.findViewById(R.id.tv_runs);
        ll_runs = view.findViewById(R.id.ll_runs);

        ll_sex_man = view.findViewById(R.id.ll_sex_man);
        ll_sex_woman = view.findViewById(R.id.ll_sex_woman);
        cl_sexo = view.findViewById(R.id.cl_sexo);
        tv_sexo = view.findViewById(R.id.tv_sexo);


        et_birth = view.findViewById(R.id.et_birth);
        iv_birth = view.findViewById(R.id.iv_birth);
        et_personal_email = view.findViewById(R.id.et_personal_email);
        ll_personal_email = view.findViewById(R.id.ll_personal_email);
        tv_personal_email = view.findViewById(R.id.tv_personal_email);

        tv_junior = view.findViewById(R.id.tv_junior);
        tv_preparatory = view.findViewById(R.id.tv_preparatory);
        tv_license = view.findViewById(R.id.tv_license);
        tv_learned_person = view.findViewById(R.id.tv_learned_person);
        tv_absence = view.findViewById(R.id.tv_absence);
        tv_learned_person = view.findViewById(R.id.tv_learned_person);
        tv_has_some = view.findViewById(R.id.tv_has_some);
        tv_not_have = view.findViewById(R.id.tv_not_have);
        tv_submit_to = view.findViewById(R.id.tv_submit_to);
    }

    public void sexSetting(){
        if(getActivity()==null){
            return;
        }
        if(sex == 0){
            ll_sex_man.setBackground(getActivity().getDrawable(R.drawable.bg_gradient_65ff96_00daf7_corner_0));
            ll_sex_woman.setBackground(getActivity().getDrawable(R.drawable.bg_frame_4d333333_corner_8));
        }else {
            ll_sex_woman.setBackground(getActivity().getDrawable(R.drawable.bg_gradient_65ff96_00daf7_corner_0));
            ll_sex_man.setBackground(getActivity().getDrawable(R.drawable.bg_frame_4d333333_corner_8));
        }
        ll_sex_man.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getActivity()==null){
                    return;
                }
                sex = 0;
                ll_sex_man.setBackground(getActivity().getDrawable(R.drawable.bg_gradient_65ff96_00daf7_corner_0));
                ll_sex_woman.setBackground(getActivity().getDrawable(R.drawable.bg_frame_4d333333_corner_8));
            }
        });
        ll_sex_woman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getActivity()==null){
                    return;
                }
                sex = 1;
                ll_sex_woman.setBackground(getActivity().getDrawable(R.drawable.bg_gradient_65ff96_00daf7_corner_0));
                ll_sex_man.setBackground(getActivity().getDrawable(R.drawable.bg_frame_4d333333_corner_8));
            }
        });
    }

    public void educationOnClick(){
        tv_junior.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                educationOn = 1;
                educationUiUpdata(tv_junior);
            }
        });

        tv_preparatory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                educationOn = 2;
                educationUiUpdata(tv_preparatory);
            }
        });

        tv_license.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                educationOn = 3;
                educationUiUpdata(tv_license);
            }
        });

        tv_learned_person.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                educationOn = 4;
                educationUiUpdata(tv_learned_person);
            }
        });

        tv_absence.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                educationOn = 5;
                educationUiUpdata(tv_absence);
            }
        });
    }

    public void educationUiUpdata(View view){
        if(getActivity()==null){
            return;
        }
        tv_junior.setBackground(getActivity().getDrawable(R.drawable.bg_frame_4d333333_corner_8));
        tv_preparatory.setBackground(getActivity().getDrawable(R.drawable.bg_frame_4d333333_corner_8));
        tv_license.setBackground(getActivity().getDrawable(R.drawable.bg_frame_4d333333_corner_8));
        tv_learned_person.setBackground(getActivity().getDrawable(R.drawable.bg_frame_4d333333_corner_8));
        tv_absence.setBackground(getActivity().getDrawable(R.drawable.bg_frame_4d333333_corner_8));
        view.setBackground(getActivity().getDrawable(R.drawable.bg_gradient_65ff96_00daf7_corner_0));
    }


    /*  ------------------ 获取数据部分 -------------------*/


    private void getZiLiPageUserInfoConfig() {
        Map<String, String> params = new HashMap<String, String>();
        String data = kycModle.getZiLiPageUserInfoConfig(params);
        try {
            JSONObject jsonObject = new JSONObject(data);
            AppKycPersonalInformacionConfig applicationUserInfoConfigBean = new AppKycPersonalInformacionConfig();
            applicationUserInfoConfigBean.initConfigData(jsonObject.optJSONObject("data"));
            AppKycPersonalInformacionConfig.UserInfoConfig userInfoConfig = applicationUserInfoConfigBean.getUserInfoConfig();
            if (applicationUserInfoConfigBean.getUserInfoConfig() != null) {
                isShowView(userInfoConfig.getFirstName(), ll_nombre, tv_nombre, "Nombre");
                isShowView(userInfoConfig.getMiddleName(), ll_father_surname, tv_father_surname, "Apellido paterno");
                isShowView(userInfoConfig.getLastName(), ll_mother_surname, tv_mother_surname, "Apellido materno (opcional)");
                isShowView(userInfoConfig.getNationalId(), ll_runs, tv_runs, "Número de RUN");
                isShowView(userInfoConfig.getGender(), cl_sexo, tv_sexo, "Sexo");
//                        isShowView(userInfoConfig.getEducationDegree(), linear_nivel_de_education, text_nivel_de_education, "Nivel de educación");
//                        isShowView(userInfoConfig.getEmail(), linear_correo_electronico_personal, text_correo_electronico_personal, "Correo electrónico personal");
                isShowView(userInfoConfig.getEmail(), ll_personal_email, tv_personal_email, "Cuenta con algunos otros préstamos vigentes?");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void getZiLiPageUserFullInfo() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("custId", ZiLiPageUserDataBean.getInstance().getCustomerId());
        String data = kycModle.getZiLiPageUserFullInfo(params);

        try {
            JSONObject jsonObject = new JSONObject(data);
            if (kycModle.isSuccess(jsonObject)) {
                showHistoryData(jsonObject.optJSONObject("data"));
            } else {
                T.showShort(getActivity(), kycModle.getStringToJson(jsonObject, "msg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void showHistoryData(JSONObject jsonObject) {
         firstName = jsonObject.optString("firstName");
         middleName = jsonObject.optString("2222");
         String lastName = jsonObject.optString("lastName");
         lastMotherName = jsonObject.optString("lastMotherName");
         nationalId = jsonObject.optString("nationalId");
         birthday = jsonObject.optString("birthday");
         gender = jsonObject.optString("gender");
         debt = jsonObject.optString("debt");

        String name =( TextUtils.isEmpty(firstName)?"":firstName )+"" +middleName;
        et_nombre.setText(name);
        et_father_surname.setText(lastName);
        et_mother_surname.setText(lastMotherName);
        et_runs.setText(nationalId);
        et_birth.setText(birthday);
        et_birth.setEnabled(false);

        if (TextUtils.equals(gender, "M")){
            sex = 1;
            sexSetting();

        }else if(TextUtils.equals(gender, "F")){
            sex = 2;
            sexSetting();
        }

        if (TextUtils.equals(debt, "Y")){
            hasSome = 1;
            hasSmome();
        }else if(TextUtils.equals(debt, "N")){
            hasSome = 2;
            hasSmome();
        }
    }

    private void userupdateUserInfo() {
        Map<String, String> params = new HashMap<String, String>();
        firstName = et_nombre.getText().toString();
        middleName = et_nombre.getText().toString();
        String lastName = et_father_surname.getText().toString();
        lastMotherName = et_mother_surname.getText().toString();
        nationalId = et_runs.getText().toString();
        birthday = et_birth.getText().toString();
//        email = et_personal_email.getText().toString();

        params.put("firstName", firstName);
        params.put("middleName", middleName);
        params.put("lastName", lastName);
        params.put("lastMotherName", lastMotherName);
        params.put("nationalId", nationalId);
        params.put("birthday", birthday);
        params.put("gender", gender);
//        params.put("educationType", educationType);
//        params.put("email", email);
        params.put("debt", debt);
        String data = kycModle.updateZiLiPageUserInfo(params);
        try {
            JSONObject jsonObject = new JSONObject(data);
            if (kycModle.isSuccess(jsonObject)) {
                CapitalInforActivity capitalInforActivity = (CapitalInforActivity) getActivity();
                if(capitalInforActivity!=null){
                    capitalInforActivity.onTabSelected(capitalInforActivity.TAG_INDEX_TWO);
                }
            } else {
                T.showShort(getActivity(), kycModle.getStringToJson(jsonObject, "msg"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
